package com.guigu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.pojo.*;
import com.guigu.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Random;

@CrossOrigin
@RestController
public class UsersController {

    //自动注入
    @Autowired
    UsersService usersService;

    //注册
    @RequestMapping("register.action")

    public Map register(Users users){

        //随机生成用账号
        Random random=new Random();
        String account="";
        //循环10位数的随机账号
        for (int i = 0; i < 10; i++) {
            account += String.valueOf(random.nextInt(10));
        }
        //给用户名设值
        users.setAccount(account);

        System.out.println(users);

        return usersService.register(users);

    }

    //判断用户名是否重复使用
    @RequestMapping("queryRepetition.action")

    public Map queryRepetition(Users users){
        System.out.println(users);
        return usersService.queryRepetition(users);
    }

    //判断邮箱||手机号是否重复使用
    @RequestMapping("queryRepetition02.action")

    public Map queryRepetition02(Users users){
        System.out.println(users);
        return usersService.queryRepetition02(users);
    }


    //登录
    @RequestMapping("login.action")

    public Users login(Users users){
        return  usersService.login(users);
    }

    //申请成为商户
    @PutMapping("apply_for_commercial.action")

    public Map apply_for_commercial(@RequestBody Users users){
        return  usersService.apply_for_commercial(users);
    }

    //申请成为供应商
    @PostMapping("apply_for_supplier.action")

    public Map apply_for_supplier(Users user,Integer[] supplierGoodsCategoryIds, MultipartFile img, HttpServletRequest request){
        return  usersService.apply_for_supplier(user,supplierGoodsCategoryIds,img,request);
    }

    //审核商户
    @PutMapping("audit_for_commercial.action")

    public Map audit_for_commercial(@RequestBody Users user){
        return  usersService.audit_for_commercial(user);
    }

    //审核供应商
    @PutMapping("audit_for_supplier.action")

    public Map audit_for_supplier(@RequestBody Users user){
        return  usersService.audit_for_supplier(user);
    }

    //分页查询
    @GetMapping("users.action")

    public Page<Users> queryAllUser(Users users,
                                             @RequestParam(value = "pageno",defaultValue = "1")Integer pageno,
                                             @RequestParam(value = "pagesize",defaultValue = "5")Integer pagesize){
        return usersService.queryAllUser(users,pageno,pagesize);
    }

    //根据id，获取对象
    @GetMapping("queryUserById.action/{id}")

    public Users queryUserById(@PathVariable Integer id){
        return usersService.queryUserById(id);
    }

    //修改供应商信息(可以修改用户名、供应商商品分类数据、营业执照)
    @PostMapping("updateSupplier.action")
    public Map updateSupplier(Integer id,String uname,Integer[] ids,MultipartFile img,HttpServletRequest request){
        return usersService.updateSupplier(id,uname,ids,img,request);
    }

    //获取所有的商户或者供应商

    @GetMapping("queryUsersByTypeId.action")
    public Page<Users> queryUsersByTypeId(Users user,
                                          @RequestParam(value = "pageno",defaultValue = "1")Integer pageno,
                                          @RequestParam(value = "pagesize",defaultValue = "5")Integer pagesize){
        return usersService.queryUsersByTypeId(user,pageno,pagesize);
    }

    //修改用户密码
    @RequestMapping("editUsersbyPwd.action")
    public Map editUsersbyPwd(Users users){


        return usersService.editUsersbyPwd(users);
    }

    //修改用户状态
    @RequestMapping("eideUserbyaccountState.action")
    public Map eideUserbyaccountState(Users users){

        return usersService.eideUserbyaccountState(users);
    }

    //根据id查询用户
    @GetMapping("usersbyid.action/{id}")
    public Users usersbyid(@PathVariable Integer id){
        return usersService.usersbyid(id);
    }

    //修改用户
    @RequestMapping("editusers.action")
    public Map editusers(Users users,MultipartFile img,HttpServletRequest request){

        return usersService.editusers(users,img,request);
    }

    //商户资料维护
    @PutMapping("updatecomm.action")
    public Map updatecomm(@RequestBody Users users){
        return usersService.editcomm(users);
    }

    //手机号的绑定
    @RequestMapping("userphonebinding.action")
    public Map userphonebinding(Users users){

        System.out.println("用户"+users);

        return usersService.userphonebinding(users);
    }

    //用户添加
    @RequestMapping("addusers.action")
    public Map addusers(Users users,MultipartFile img,HttpServletRequest request){

        System.out.println("图片---"+img);
        return usersService.addusers(users,img,request);
    }
    //商户开关店
    @PutMapping("updateBYisCloseUpShop.action")
    public Map updateBYisCloseUpShop(@RequestBody Users users){
        return usersService.updateBYisCloseUpShop(users);
    }
}
